package com.tjetc.service.impl;

import com.tjetc.constant.CommonConstant;
import com.tjetc.dao.PayTypeDao;
import com.tjetc.dao.impl.PayTypeDaoImpl;
import com.tjetc.exception.MessageException;
import com.tjetc.pojo.dto.PayDTO;
import com.tjetc.pojo.vo.PayTypeVO;
import com.tjetc.service.PayService;
import com.tjetc.utils.DruidUtils;
import com.tjetc.utils.RedisUtils;
import redis.clients.jedis.Jedis;

import java.util.List;

/**
 * @Author: itda
 * @Date: 2024/5/13 11:44
 * @Description: PayServiceImpl
 */
public class PayServiceImpl implements PayService {

    private PayTypeDao payTypeDao = new PayTypeDaoImpl();

    @Override
    public void save(String payName) {
        try {
            PayTypeVO payTypeVO = payTypeDao.getByName(payName);
            if (payTypeVO != null) {
                throw new MessageException(CommonConstant.PAY_METHOD_EXIST);
            }
            DruidUtils.start();
            payTypeDao.insert(payName);
            DruidUtils.commit();
            System.out.println(CommonConstant.ADD_SUCCESS);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            DruidUtils.rollback();
        }
    }

    @Override
    public void delete(String payName) {
        try {
            PayTypeVO payTypeVO = payTypeDao.getByName(payName);
            if (payTypeVO == null) {
                throw new MessageException(CommonConstant.PAY_METHOD_NO_EXIST);
            }
            DruidUtils.start();
            payTypeDao.delete(payName);
            DruidUtils.commit();
            System.out.println(CommonConstant.DELETE_SUCCESS);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            DruidUtils.rollback();
        }
    }

    @Override
    public void update(PayDTO payDTO) {
        try {
            PayTypeVO p1 = payTypeDao.getByName(payDTO.getOldPayName());
            PayTypeVO p2 = payTypeDao.getByName(payDTO.getNewPayName());
            if (p1 == null) {
                throw new MessageException(CommonConstant.PAY_METHOD_NO_EXIST);
            }
            if (p2 != null) {
                throw new MessageException(p2.getName() + CommonConstant.PAY_METHOD_EXIST);
            }
            DruidUtils.start();
            payTypeDao.update(payDTO);
            DruidUtils.commit();
            System.out.println(CommonConstant.UPDATE_SUCCESS);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            DruidUtils.rollback();
        }
    }

    @Override
    public void list() {
        try {
            List<PayTypeVO> list = payTypeDao.list();
            if (list==null||list.isEmpty()){
                throw new MessageException(CommonConstant.NO_FIND_DATA);
            }
            list.forEach(System.out::println);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    @Override
    public PayTypeVO getByName(String payName) {
        try {
            return payTypeDao.getByName(payName);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return null;
    }

    /*private Jedis = RedisUtils.getJedis();

    @Override
    public void save(String payName) {
        int score = Integer.parseInt(jedis.get("keyScore"));
        if (jedis.zadd("payment", ++score, payName) > 0) {
            System.out.println(CommonConstant.ADD_SUCCESS);
            jedis.set("keyScore", Integer.toString(score));
        } else {
            try {
                throw new MessageException(CommonConstant.PAY_METHOD_EXIST);
            } catch (MessageException e) {
                System.out.println(e.getMessage());
            }
        }
        *//*if (jedis.smembers("pay").contains(payName)) {
            throw new MessageException(CommonConstant.PAY_METHOD_EXIST);
        }
        jedis.sadd("pay", payName);
        System.out.println(CommonConstant.ADD_SUCCESS);*//*
    }

    @Override
    public void delete(String payName) {
        if (jedis.zrem("payment", payName) > 0) {
            System.out.println(CommonConstant.DELETE_SUCCESS);
        } else {
            try {
                throw new MessageException(CommonConstant.DELETE_ERROR);
            } catch (MessageException e) {
                System.out.println(e.getMessage());
            }
        }
        *//*jedis.srem("pay", payName);
        System.out.println(CommonConstant.DELETE_SUCCESS);*//*
    }

    @Override
    public void update(PayDTO payDTO) {
        Double zscore = jedis.zscore("payment", payDTO.getOldPayName());
        if (zscore>0){
            jedis.zadd("payment",zscore,payDTO.getNewPayName());
            System.out.println(CommonConstant.UPDATE_SUCCESS);
        }else {
            try {
                throw new MessageException(CommonConstant.UPDATE_ERROR);
            } catch (MessageException e) {
                System.out.println(e.getMessage());
            }
        }
            *//*if (!jedis.smembers("pay").contains(payDTO.getOldPayName())) {
                throw new MessageException(CommonConstant.PAY_METHOD_NO_EXIST);
            }
            //删除旧支付方式
            jedis.srem("pay", payDTO.getOldPayName());
            //新增新支付方式
            jedis.sadd("pay", payDTO.getNewPayName());
            System.out.println(CommonConstant.UPDATE_SUCCESS);*//*
    }

    @Override
    public void list() {
        jedis.smembers("pay").forEach(System.out::println);
    }*/
}
